<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <div class="mui-card" v-for="(item, i) in shopcarlist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch v-model="$store.getters.getGoodsSelectedChange[item.id]" @change="goodsSelectedChange(item.id, $store.getters.getGoodsSelectedChange[item.id])"></mt-switch>
            <img :src="item.img_url">
            <div class="info">
              <h2>{{ item.title }}</h2>
              <p>
                <span class="price">￥{{ item.sell_price }}</span>
                <numbox :count="$store.getters.getGoodsCount[item.id]" :id="item.id"></numbox>
                <a href="#" @click.prevent="removeGoods(item.id, i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{ $store.getters.getGoodsAllAmount.count }}</span> 件，总价
              <span class="red">￥{{ $store.getters.getGoodsAllAmount.amount }}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    {{ $store.getters.getGoodsSelectedChange }}
  </div>
</template>

<script>
import numbox from "../subcomponents/ShopCarNumBox.vue";

export default {
  data() {
    return {
      shopcarlist: []
    };
  },
  created() {
    this.getGoodsCarList();
  },
  methods: {
    getGoodsCarList() {
      var idArr = [];
      // 遍历 store 中商品的id，放到idArr
      this.$store.state.car.forEach(item => idArr.push(item.id));
      this.$http.get("goodscarlist.php?id[]=" + idArr).then(result => {
        if (result.body.status === 0) {
          this.shopcarlist = result.body.message;
        }
      });
    },
    removeGoods(id, index) {
      this.shopcarlist.splice(index, 1);
      this.$store.commit("removeGoodsInfo", id);
    },
    goodsSelectedChange(id, val) {
      this.$store.commit("updateGoodsSelectedChange", { id, selected: val })
    }
  },
  components: {
    numbox
  }
};
</script>

<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;

  .goods-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
      img {
        width: 60px;
        height: 60px;
      }
      h2 {
        font-size: 13px;
      }
      .info {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .price {
          color: red;
          font-weight: bold;
        }
      }
    }
  }

  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .red {
      color: red;
      font-weight: bold;
      font-size: 16px;
    }
  }
}
</style>
